package com.pro.service.imple;
import com.pro.mapper.JsAuthorityMapper;
import com.pro.mapper.JsLoginUserMapper;
import com.pro.pojo.JsUser;
import com.pro.pojo.LoginUser;
import com.pro.service.JsLoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName:SecurityServiceImple
 * Package:com.pro.service.imple
 * Description:
 *
 * @Date:2022/1/8 20:36
 * @Author: XJR
 */
@Service
public class SecurityServiceImple implements UserDetailsService {

    @Autowired
    private JsLoginUserMapper mapper;

    @Autowired
    private JsAuthorityMapper jsAuthorityMapper;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        JsUser jsUser = mapper.loginByName(username);
        if (jsUser == null){
            throw new UsernameNotFoundException("用户名出错误");
            //三个参数为：用户名、用户密码、用户权限集合（没有权限，创建一个空的集合）
        }
        List<String> list = jsAuthorityMapper.getPermsByUserId(jsUser.getUserId());
        return new LoginUser(jsUser,list);
    };
}
